package connectorTests;

import static org.junit.Assert.assertEquals;

import org.junit.BeforeClass;
import org.junit.Test;

import connectors.Client;
import connectors.Connection;
import connectors.Server;
/**
 * 
 * @author Lucas 'Mezmor' Pfister, Vishal Patel
 *
 */
public class ConnectionTests {
	private static class ServerThread extends Thread {
		int port;
		Connection server;
		
		public ServerThread(int port){
			this.port = port;
		}
		
		@Override
		public void run(){
			server = new Server(port);
			while(!Thread.interrupted()){
				String data = server.receive();
				if(data.equals("Ping"))
					server.send("Pong");
				else if(data.equals("LongTransmit")){
					server.send("TransmitLong");
				}
			}
		}
	}
	
	private static ServerThread server;
	private static Connection client;
	
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		server = new ServerThread(1234);
		server.start();
		client = new Client("localhost", 1234);
	}
	
	@Test
	public void testPingPong(){
		client.send("Ping");
		String data = client.receive();
		assertEquals("Pong", data);
	}
	
	@Test
	public void testWaitForData(){
		client.send("LongTransmit");
		String data = client.receive();
		assertEquals("TransmitLong", data);
	}
}